using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LoadManager : MonoBehaviour
{
	public LoadView loadView;
	public List<ExoSaveData> exoSavedData;
	public Texture2D tex;
	public GameObject noDataPrefab;
	public GameObject withDataPrefab;

	// Use this for initialization
	void Start ()
	{
		LoadLoadDisplay();
	}

	void LoadLoadDisplay()
	{
		exoSavedData = new List<ExoSaveData>();
		//screenshotPhotos = new List<byte[]>();
		loadView = GameObject.Find ("SaveLoadManager").GetComponent<LoadView>();
		for(int i = 0; i < 8; i++)
		{
			ExoSaveData loadedData = new ExoSaveData();
			
			string loadDataPath = Application.persistentDataPath + "/SaveFile" + (i + 1);
			if(SerializeUtility.IsFileExist(loadDataPath))
			{
				loadedData = (ExoSaveData)SerializeUtility.LoadObject(loadDataPath, typeof(ExoSaveData)); //this would likely lead to an error if data file does not exist
				exoSavedData.Add(loadedData);
				Debug.Log("Previous Data added!");
				
				//screenshotPhotos.Add(System.IO.File.ReadAllBytes(loadDataPathPhoto));
			}
			else
			{
				ExoSaveData e = new ExoSaveData();
				exoSavedData.Add(e);
			}
		} //8 corresponds to loadLimit
		for(int i = 0; i < exoSavedData.Count; i++)
		{

			if(exoSavedData[i].currentChapter > 0)
			{
				GameObject go = Instantiate(withDataPrefab) as GameObject;
				SetUpWithDataDisplay(go, exoSavedData[i], i);
			}
			else
			{
				GameObject go = Instantiate(noDataPrefab) as GameObject;
				SetUpNoDataDisplay(go, i);
			}
		}
	}

	void SetUpWithDataDisplay(GameObject go, ExoSaveData esd, int i)
	{
		UILabel WithDataText = go.transform.FindChild("Label").GetComponent<UILabel>();
		SpriteRenderer WithDataScreenshot = go.transform.FindChild("bla").GetComponent<SpriteRenderer>();

		if(i < 4)
			go.transform.parent = loadView.LoadPanel1.transform;
		else
			go.transform.parent = loadView.LoadPanel2.transform;

		WithDataText.text = exoSavedData[i].DateOfSave + "\n" + exoSavedData[i].TimeOfSave + "\nChapter " + exoSavedData[i].currentChapter;
		tex = new Texture2D((int)WithDataScreenshot.sprite.rect.width,(int)WithDataScreenshot.sprite.rect.height);
		tex.LoadImage(System.IO.File.ReadAllBytes(Application.persistentDataPath + "/SaveFile" + (i + 1) + "Photo"));
		WithDataScreenshot.sprite = Sprite.Create(tex, WithDataScreenshot.sprite.rect, new Vector2(0.5f, 0.5f));
		WithDataScreenshot.transform.localScale = new Vector3(5.0f, 5.0f, 5.0f);
		WithDataScreenshot.name = i.ToString();

		if(i < 4)
			go.transform.localPosition = new Vector3(0.0f, (-115.0f * i), -0.09999999f);
		else
			go.transform.localPosition = new Vector3(0.0f, (-115f * (i - 4)), -0.09999999f);

		go.transform.localScale = new Vector3(1.742176f, 1.742176f, 1.742176f);
		go.GetComponent<UIButtonMessage>().target = GameObject.Find("SaveLoadManager");
		go.GetComponent<UIButtonMessage>().functionName = "ShowLoadSave";
		go.name = "WithData" + (i + 1);
	}

	void SetUpNoDataDisplay(GameObject go, int i)
	{
		if(i < 4)
			go.transform.parent = loadView.LoadPanel1.transform;
		else
			go.transform.parent = loadView.LoadPanel2.transform;

		if(i < 4)
			go.transform.localPosition = new Vector3(0.0f, (-115.0f * i), -0.09999999f);
		else
			go.transform.localPosition = new Vector3(0.0f, (-115.0f * (i - 4)), -0.09999999f);

			go.transform.localScale = new Vector3(0.6f, 0.6f, 0.6f);
		go.name = "NoData" + (i + 1);
	}

	public void RefreshLoadList()
	{
		Collider[] col1 = loadView.LoadPanel1.GetComponentsInChildren<Collider>();
		Collider[] col2 = loadView.LoadPanel2.GetComponentsInChildren<Collider>();

		for(int i = 0; i < col1.Length; i++)
			Destroy (col1[i].gameObject);
		for(int i = 0; i < col2.Length; i++)
			Destroy (col2[i].gameObject);

		LoadLoadDisplay();
	}
}
